package com.cn.servlet.book;

import com.cn.Dao.BookDao;
import com.cn.Dao.NewsinfoDao;
import com.cn.entity.BookInfo;
import com.cn.entity.NewsInfo;
import com.cn.entity.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.sql.SQLException;

@WebServlet(name = "InsertBookServlet")
public class InsertBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message="";
        String id = "";
        String copy = "";
        String month = "";
        request.setCharacterEncoding("GBK");
        response.setCharacterEncoding("GBK");
        try {
            BookDao bookDao=new BookDao();
            BookInfo bookInfo= new BookInfo();
            UserInfo userInfo=(UserInfo)request.getSession().getAttribute("userInfo");
            id=request.getParameter("num");
            String price=request.getParameter("price");
            month=request.getParameter("month");
            copy=request.getParameter("copy");
            Date day=new Date();
            bookInfo.setUserid(userInfo.getId());
            bookInfo.setState(2);
            bookInfo.setTime(day);
            bookInfo.setTotal_price(Float.valueOf(price));
            bookInfo.setMouth(Integer.valueOf(month));
            bookInfo.setAmount(Integer.valueOf(copy));
            bookInfo.setNum(id);
            System.out.println(bookInfo.getUserid());
            System.out.println(id);
            bookDao.Insert(bookInfo);
            message="下单成功";
            System.out.println("下单成功");
            request.setAttribute("message",message);
            request.getRequestDispatcher("all_newspaper").forward(request, response);
        }catch (SQLException throwables) {
            throwables.printStackTrace();
            message="下单失败";
            System.out.println(id);
            System.out.println("下单失败");
            String url="check_order.jsp?ID="+id+"&copies="+copy+"&months="+month;
            System.out.println(url);
            request.setAttribute("message",message);
            request.getRequestDispatcher(url).forward(request, response);
        }
        finally {
            request.setAttribute("message", message);
        }
    }
}
